﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataTableDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tag++;
            y += 30;
            AddLab(13, 33 + y, "labArrOsName", "品牌：");
            AddLab(190, 33 + y, "labArrOsName", "物品名称：");
            AddLab(425, 33 + y, "labArrOsNum", "数量：");
            AddLab(594, 33 + y, "labArrOsDeclaredValue", "单价：");
            AddLab(768, 33 + y, "label23", "总价：");
            AddTxtWidths(76, 30 + y, "Brand");
            AddTxtWidth(284, 30 + y, "GoodsName");
            AddTxts(487, 30 + y, "Num");//数量*单价这两个控件需要添加事件
            AddTxts(655, 30 + y, "Price");
            AddTxt(830, 30 + y, "TotalPrice");
        }

        int y = 0;
        int tag = 0;

        /// <summary>
        /// 添加商品购买的文本
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="name"></param>
        /// <param name="text"></param>
        private void AddLab(int x, int y, string name, string text)
        {

            System.Windows.Forms.Label label = new System.Windows.Forms.Label();
            label.AutoSize = true;
            label.Location = new System.Drawing.Point(x, y);
            label.Name = name;
            label.Size = new System.Drawing.Size(41, 12);
            label.Text = text;
            groupBox1.Controls.Add(label);
        }
        private void AddTxtWidth(int x, int y, string name)
        {
            tag++;
            TextBox txt = new TextBox();
            txt.Tag = tag;
            txt.Location = new System.Drawing.Point(x, y);
            txt.Name = name;
            txt.Size = new System.Drawing.Size(139, 21);
            groupBox1.Controls.Add(txt);
        }
        private void AddTxtWidths(int x, int y, string name)
        {
            tag++;
            TextBox txt = new TextBox();
            txt.Tag = tag;
            txt.Location = new System.Drawing.Point(x, y);
            txt.Name = name;
            txt.Size = new System.Drawing.Size(108, 21);
            groupBox1.Controls.Add(txt);
        }
        private void AddTxt(int x, int y, string name)
        {
            tag++;
            TextBox txt = new TextBox();
            txt.Tag = tag;
            txt.Location = new System.Drawing.Point(x, y);
            txt.Name = name;
            txt.Size = new System.Drawing.Size(100, 21);
            groupBox1.Controls.Add(txt);
        }

        /// <summary>
        /// 给创建控件的方法添加事件
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="name"></param>
        private void AddTxts(int x, int y, string name)
        {
            TextBox txt = new TextBox();
            txt.Tag = tag;
            txt.Location = new System.Drawing.Point(x, y);
            txt.Name = name;
            txt.Size = new System.Drawing.Size(100, 21);
            groupBox1.Controls.Add(txt);
            //添加事件
            txt.TextChanged += new EventHandler(Nums_TextChanged);
        }
        /// <summary>
        /// 自定义添加控件执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Nums_TextChanged(object sender, EventArgs e)
        {
            TextBox btn = (TextBox)sender;
            int tagIndex = Convert.ToInt32(btn.Tag);
            decimal CountNum = 0;
            if (btn.Name == "Num")
            {
                Control[] nums = groupBox1.Controls.Find("Price", true);
                Control[] Total = groupBox1.Controls.Find("TotalPrice", true);
                for (int i = 0; i < nums.Length; i++)
                {
                    if (Convert.ToInt32(nums[i].Tag) == tagIndex)
                    {
                        CountNum = Convert.ToDecimal(btn.Text) * Convert.ToDecimal(nums[i].Text);
                        Total[i].Text = CountNum.ToString();
                    }
                }
            }
            else if (btn.Name == "Price")
            {
                Control[] nums = groupBox1.Controls.Find("Num", true);
                Control[] Total = groupBox1.Controls.Find("TotalPrice", true);
                for (int i = 0; i < nums.Length; i++)
                {
                    if (Convert.ToInt32(nums[i].Tag) == tagIndex)
                    {
                        CountNum = Convert.ToDecimal(btn.Text) * Convert.ToDecimal(nums[i].Text);
                        Total[i].Text = CountNum.ToString();
                    }
                }
            }

            Control[] TotalPrice = groupBox1.Controls.Find("TotalPrice", true);
            decimal Totalnum = 0;
            for (int i = 0; i < TotalPrice.Length; i++)
            {
                Totalnum += Convert.ToDecimal(TotalPrice[i].Text);

            }
            ZongPrice = Totalnum.ToString();
            Control[] Num = groupBox1.Controls.Find("Num", true);
            decimal shuliang = 0;
            for (int i = 0; i < Num.Length; i++)
            {
                shuliang += Convert.ToDecimal(Num[i].Text);

            }
            Nums = shuliang.ToString();

            Control[] Price = groupBox1.Controls.Find("Price", true);
            decimal danjia = 0;
            for (int i = 0; i < Price.Length; i++)
            {
                danjia += Convert.ToDecimal(Price[i].Text);

            }
            Prices = danjia.ToString();
        }

        //第一行固定格式的数量改变的事件

        private void Num_TextChanged(object sender, EventArgs e)
        {
            decimal CountNum = Convert.ToDecimal(this.Num.Text) * Convert.ToDecimal(this.Price.Text);
            this.TotalPrice.Text = CountNum.ToString();

            Control[] TotalPrice = groupBox1.Controls.Find("TotalPrice", true);
            decimal Totalnum = 0;
            for (int i = 0; i < TotalPrice.Length; i++)
            {
                Totalnum += Convert.ToDecimal(TotalPrice[i].Text);

            }
            ZongPrice = Totalnum.ToString();

            Control[] Num = groupBox1.Controls.Find("Num", true);
            decimal shuliang = 0;
            for (int i = 0; i < Num.Length; i++)
            {
                shuliang += Convert.ToDecimal(Num[i].Text);

            }
            Nums = shuliang.ToString();

            Control[] Price = groupBox1.Controls.Find("Price", true);
            decimal danjia = 0;
            for (int i = 0; i < Price.Length; i++)
            {
                danjia += Convert.ToDecimal(Price[i].Text);

            }
            Prices = danjia.ToString();
        }

    }
}
